package servlets;


import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domein.AutoTotaalDiensten;
import domein.Persoon;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name="LoginServlet", urlPatterns="/Login.do")
public class LoginServlet extends HttpServlet   {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		String mail = req.getParameter("email"); 
		String password = req.getParameter("wachtwoord");
		RequestDispatcher rd = null;
 		Persoon p = AutoTotaalDiensten.login(mail, password);
		if (p == null) {
			req.setAttribute("error", "Gebruikersnaam of wachtwoord incorrect.");
			rd = req.getRequestDispatcher("/login.jsp");
			//resp.sendRedirect("login.jsp?error=+Gebruikersnaam+en+of+wachtwoord+incorrect.");
			//System.out.print("Inloggen mislukt");
		}
		else{
			rd = req.getRequestDispatcher("/loggedin.jsp");			
			req.getSession().setAttribute("user", p);
		}
		rd.forward(req, resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
